package com.ruoyi.prod.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.prod.domain.Prod;
import com.ruoyi.prod.service.IProdService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author nz
 * @date 2025-08-09
 */
@RestController
@RequestMapping("/prod")
public class ProdController extends BaseController
{
    @Autowired
    private IProdService prodService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("prod:prod:list")
    @GetMapping("/list")
    public TableDataInfo list(Prod prod)
    {
        startPage();
        List<Prod> list = prodService.selectProdList(prod);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("prod:prod:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prod prod)
    {
        List<Prod> list = prodService.selectProdList(prod);
        ExcelUtil<Prod> util = new ExcelUtil<Prod>(Prod.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("prod:prod:query")
    @GetMapping(value = "/{prodId}")
    public AjaxResult getInfo(@PathVariable("prodId") Long prodId)
    {
        return success(prodService.selectProdByProdId(prodId));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("prod:prod:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prod prod)
    {
        return toAjax(prodService.insertProd(prod));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("prod:prod:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prod prod)
    {
        return toAjax(prodService.updateProd(prod));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("prod:prod:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prodIds}")
    public AjaxResult remove(@PathVariable Long[] prodIds)
    {
        return toAjax(prodService.deleteProdByProdIds(prodIds));
    }

    /**
     * 商品es列表
     * @param prod
     * @return
     */
    @GetMapping(value = "/getEsProdList")
    public TableDataInfo getEsProdList(Prod prod){
        String pageNum1 = request.getParameter("pageNum");
        String pageSize1 = request.getParameter("pageSize");
        Integer pageNum = Integer.valueOf(pageNum1);
        Integer pageSize = Integer.valueOf(pageSize1);
        return prodService.getEsProdList(pageNum,pageSize,prod);
    }

    /**
     * 创建商品索引
     * @return
     */
    @GetMapping(value = "/createIndex")
    public AjaxResult createIndex(){
        return prodService.createIndex();
    }

    /**
     * 同步es
     * @return
     */
    @GetMapping(value = "/getProdList")
    public AjaxResult getProdList(){
        return prodService.getProdList();
    }
}
